package cn.maka.dao.impl;

import cn.maka.dao.UserDAO;
import cn.maka.domain.User;
import cn.maka.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserDaoImpl implements UserDAO {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<User> findAll() {
        String sql = "select * from user";
        List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class));

        return users;
    }

    public User findUserByUsernameAndPassword(String username, String password) {
        try {
            String sql = "select * from user where username = ? and password = ?";
            User user = template.queryForObject(sql,
                    new BeanPropertyRowMapper<User>(User.class),
                    username, password);

            return user;
        } catch (DataAccessException e) {
            //e.printStackTrace();
            return null;
        }
    }

    @Override
    public User findUserById(int id) {
        String sql = "select * from user where id = ?";
        User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
        return user;
    }

    @Override
    public void add(User user) {
        String sql="insert into user values(null,?,?,?,?,?,?,?)";
        template.update(sql,user.getUsername(),user.getPassword(),user.getGender(),user.getAge(),user.getAddress(),user.getQq(),user.getEmail());

    }

    @Override
    public void del(int id) {
        String sql="delete from user where id=?";

        template.update(sql,id);
    }

    @Override
    public void update(User user) {
        String sql="update user set gender = ?,age = ?,address = ?,qq = ?,email = ? where id = ?";
        Object[] objects = {
                user.getGender(),
                user.getAge(),
                user.getAddress(),
                user.getQq(),
                user.getEmail(),
                user.getId()
        };
        int updateNum = template.update(sql, objects);
    }

    @Override
    public int findTotalCount(Map<String, String[]> condition) {
        String sql = "select count(*) from user where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);

        List<String> objects = new ArrayList<>();

        Set<String> keySet = condition.keySet();
        for (String s : keySet) {
            if ("currentPage".equals(s) || "rows".equals(s)){
                continue;
            }

            String value = condition.get(s)[0];

            if ("".equals(value) || value==null){
                continue;
            }

            sb.append(" and "+s+" like ?");

            objects.add("%"+value+"%");
        }

//        System.out.println(sb.toString());
//        System.out.println(objects);

        return template.queryForObject(sb.toString(),Integer.class,objects.toArray()) ;
    }

    @Override
    public List<User> findByPage(int start, int intRows, Map<String, String[]> condition) {
        String sql = "select * from user where 1 = 1 ";

        StringBuilder sb = new StringBuilder(sql);

        List<Object> objects = new ArrayList<>();

        Set<String> keySet = condition.keySet();
        for (String s : keySet) {
            if ("currentPage".equals(s) || "rows".equals(s)){
                continue;
            }

            String value = condition.get(s)[0];

            if ("".equals(value) || value==null){
                continue;
            }

            sb.append(" and "+s+" like ? ");

            objects.add("%"+value+"%");
        }

        sb.append(" limit ? , ? ");

        objects.add(start);
        objects.add(intRows);

        sql = sb.toString();

        return template.query(sql,new BeanPropertyRowMapper<User>(User.class),objects.toArray());
    }

}
